package config

import "sync"

/**
 * description：
 * <p>
 * </p>
 * change history:
 * date                      defect#    person       comments
 * -------------------------------------------------------------<p>
 * 2023/10/10 18:20          ********    kong.ruo    create file.
 *
 * @author kong.ruo
 * @date 2023/10/10 18:20
 *
 * @since go 1.20
 */

var chainInstance *ChainResource
var handleChain sync.Once

type ChainResource struct {
	chainList []Chain
}

func GetChainResource() *ChainResource {
	handleChain.Do(func() {
		chainInstance = &ChainResource{chainList: make([]Chain, 0)}
	})
	return chainInstance
}

func (cr *ChainResource) AddFilter(chain Chain) {
	cr.chainList = append(cr.chainList, chain)
}

func (cr *ChainResource) getChain() []Chain {
	return cr.chainList
}

func (cr *ChainResource) Handle(in *FilterChainIn) bool {
	for _, chain := range cr.getChain() {
		return chain.HandleChain(in)
	}
	return true
}
